import { Clock } from '$lib/clock';
import { Creature, Glyph, Impassable } from '$lib/components';
import { Heart } from '$lib/content/heart';
import { Types, defineComponent, hasComponent } from '$lib/ecs';
import { Eyes } from '$lib/eyes';
import { Move3Action } from '$lib/walk3';
import { SelectedMeleeStyle } from './melee';
import { Stamina, StaminaRegen } from './stamina';
import { BloodLevel } from './blood';
import { Head, LeftArm, LeftLeg, RightArm, RightLeg, Torso } from './body';
import { BodySlot, FeetSlot, HandsSlot, HeadSlot, LegsSlot, MainHandSlot, NeckSlot, OffHandSlot } from './equipment';

export const Human = defineComponent('Human', {});

export const humanTemplate = [
  [Creature],
  [Human],
  [Glyph, {
    glyph: '@',
  }],
  [Eyes],
  [Heart, {
    heartbeat: 10,
    elapsed: 0,
  }],
  [Clock],

  [Head],
  [Torso],
  [LeftArm],
  [RightArm],
  [LeftLeg],
  [RightLeg],

  [Stamina],
  [StaminaRegen],

  [BloodLevel],

  [HeadSlot],
  [HandsSlot],
  [FeetSlot],
  [LegsSlot],
  [BodySlot],
  [NeckSlot],
  [MainHandSlot],
  [OffHandSlot],

  [SelectedMeleeStyle],
];

export const humanCanPass = (world, es) => {
  return (es.length > 0) && !es.some((e) => hasComponent(world, Creature, e) || hasComponent(world, Impassable, e));
};

export const HumanMove = (dx, dy, dz) => (world, e) => {
  const delay = Heart.heartbeat[e];
  return Move3Action(humanCanPass)(dx, dy, dz, delay)(world, e);
};
